package com.cd.music.service.impl;

import com.cd.music.dao.NewsDao;
import com.cd.music.dto.ContentDto;
import com.cd.music.dto.ResultBean;
import com.cd.music.model.News;
import com.cd.music.service.NewsService;
import com.cd.music.utils.IdUtil;
import com.cd.music.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yxl 余秀良
 * @version 1.0
 * @description: 用途
 * @date 2018年03月07日  上午9:24
 * @address: 四川成都
 */
@Transactional(readOnly = true, rollbackFor = Exception.class)
@Service(value = "newsService")
public class NewsServiceImpl implements NewsService {
    @Resource
    private NewsDao newsDao;

    /**
     * 添加新闻
     *
     * @param dto 参数
     * @return 受影响行数
     * @author yxl 余秀良
     * @version 1.0
     * @description: 添加新闻
     * @date 2018年03月06日 下午10:20
     * @address: 四川成都
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultBean insert(ContentDto dto) {
        ResultBean.Builder builder = ResultBean.builder();
        if (dto != null) {
            String title = dto.getTitle();
            String summary = dto.getSummary();
            String coverImage = dto.getCoverImage();
            String content = dto.getContent();
            if (!StringUtil.isEmpty(title) && !StringUtil.isEmpty(summary) && !StringUtil.isEmpty(coverImage) && !StringUtil.isEmpty(content)) {
                News news1 = new News();
                news1.setId(IdUtil.getUUID());
                news1.setTitle(dto.getTitle());
                news1.setContent(dto.getContent());
                news1.setCoverImage(dto.getCoverImage());
                news1.setSummary(dto.getSummary());
                news1.setDelete(false);
                boolean isOK = newsDao.insert(news1) > 0;
                builder.success(isOK).message(isOK ? "添加成功" : "添加失败");
            } else {
                builder.code(400).message("请检查参数").success(false);
            }
        }
        return builder.build();
    }

    /**
     * 删除新闻
     *
     * @param id id
     * @return 受影响行数
     * @author yxl 余秀良
     * @version 1.0
     * @description: 删除新闻
     * @date 2018年03月06日 下午10:20
     * @address: 四川成都
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultBean delete(String id) {
        ResultBean.Builder builder = ResultBean.builder();
        if (!StringUtil.isEmpty(id)) {
            boolean isOK = newsDao.delete(id) > 0;
            builder.success(isOK).message(isOK ? "成功" : "失败");
        } else {
            builder.code(400).message("请检查参数").success(false);
        }
        return builder.build();
    }

    /**
     * 修改新闻
     *
     * @param dto 参数
     * @return 受影响行数
     * @author yxl 余秀良
     * @version 1.0
     * @description: 删除新闻
     * @date 2018年03月06日 下午10:20
     * @address: 四川成都
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultBean update(ContentDto dto) {
        ResultBean.Builder builder = ResultBean.builder();
        if (dto != null) {
            News news = new News();
            news.setId(dto.getId());
            news.setTitle(dto.getTitle());
            news.setSummary(dto.getSummary());
            news.setCoverImage(dto.getCoverImage());
            news.setContent(dto.getContent());
            boolean isOK = newsDao.update(news) > 0;
            builder.success(isOK).message(isOK ? "成功" : "失败");
        } else {
            builder.code(400).message("请检查参数").success(false);
        }
        return builder.build();
    }

    /**
     * 查询新闻
     *
     * @param dto 参数
     * @return 新闻列表
     * @author yxl 余秀良
     * @version 1.0
     * @description: 查询新闻
     * @date 2018年03月06日 下午10:20
     * @address: 四川成都
     */
    @Override
    public ResultBean select(ContentDto dto) {
        ResultBean.Builder builder = ResultBean.builder();
        if (dto != null) {
            if (dto.getPageIndex() != null && dto.getPageSize() != null) {
                Integer index = dto.getPageIndex();
                Integer pageSize = dto.getPageSize();
                Integer pageIndex = (index <= 0) ? 0 : ((index - 1) * pageSize);
                dto.setPageIndex(pageIndex);
                Integer total = newsDao.selectNewsTotal();
                List<Map> select = newsDao.select(dto);
                Map<String, Object> map = new LinkedHashMap<>();
                map.put("total", total);
                map.put("pageIndex", index);
                map.put("list", select);
                builder.data(map).message(select.size() > 0 ? "成功" : "没有数据").success(true);
            } else {
                builder.code(400).message("请检查参数").success(false);
            }
        } else {
            builder.code(400).message("请检查参数").success(false);
        }
        return builder.build();
    }

    @Override
    public ResultBean userQueryNewsList(ContentDto dto) {
        ResultBean.Builder builder = ResultBean.builder();
        Integer index = 1;
        Integer pageSize = 10;
        Integer pageIndex;
        if (dto != null) {
            if (dto.getPageIndex() != null && dto.getPageSize() != null) {
                index = dto.getPageIndex();
                pageSize = dto.getPageSize();
                pageIndex = (index <= 0) ? 0 : ((index - 1) * pageSize);
                dto.setPageIndex(pageIndex);
            }
        } else {
            dto = ContentDto.builder().build();
            pageIndex = (index <= 0) ? 0 : ((index - 1) * pageSize);
            dto.setPageIndex(pageIndex);
        }
        Integer total = newsDao.selectNewsTotal();
        List<Map> select = newsDao.select(dto);
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("total", total);
        map.put("pageIndex", index);
        map.put("list", select);
        builder.data(map).message(select.size() > 0 ? "成功" : "没有数据").success(true);
        return builder.build();
    }

    /**
     * 查询单个新闻
     *
     * @param dto 参数
     * @return 新闻信息
     * @author yxl 余秀良
     * @version 1.0
     * @description: 查询单个新闻
     * @date 2018年03月06日 下午10:20
     * @address: 四川成都
     */
    @Override
    public ResultBean selectOneNews(ContentDto dto) {
        ResultBean.Builder builder = ResultBean.builder();
        if (dto != null) {
            String id = dto.getId();
            if (!StringUtil.isEmpty(id)) {
                News news = newsDao.selectOneNews(id);
                builder.message(news != null ? "成功" : "没有数据").data(news).success(true);
            } else {
                builder.code(400).message("缺少必要参数").success(false);
            }
        } else {
            builder.success(false).code(400).message("参数不能为空");
        }
        return builder.build();
    }
}
